package com.thingsgrid.device.monitor.service.impl;
import com.thingsgrid.device.entity.DeviceRecord;
import com.thingsgrid.device.entity.Product;
import com.thingsgrid.device.entity.ProductType;
import com.thingsgrid.device.monitor.service.IRunAnalyzeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.List;

@Service
public class RunAnalyzeServiceImpl implements IRunAnalyzeService {

	@Autowired
	private MongoTemplate mongoTemplate;

	/**
	 *
	 *查询产品的所有类型
	 * @author 陈学栋
	 * @date 2019/8/21 14:35
	 */
	@Override
	public List<ProductType> selectclassify() {
		List<ProductType> productTypeList = mongoTemplate.find(new Query(), ProductType.class, "product_type");
		return productTypeList;
	}

	/**
	 *
	 *根据产品类型的Id查询与之相关联的产品
	 * @author 陈学栋
	 * @date 2019/8/21 14:36
	 */
	@Override
	public List<Product> findById(String productTypeId) {
		Query query = new Query();
		query.addCriteria(Criteria.where("productTypeId").is(productTypeId));
		List<Product> productList = mongoTemplate.find(query, Product.class, "product");
		return productList;
	}

	/**
	 *
	 *查询时间段内在线设备数量
	 * @author 陈学栋
	 * @date 2019/8/21 14:37
	 */
	@Override
	public LinkedHashMap<String, Integer> countOnlieDeviceById(String productId, Integer selectTime)  {
		Calendar calendar = Calendar.getInstance();
		calendar.set(Calendar.HOUR_OF_DAY, calendar.get(Calendar.HOUR_OF_DAY) - selectTime);
		SimpleDateFormat format  = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		LinkedHashMap<String, Integer> hashMap = new LinkedHashMap<>();
		//查询在线设备的开始时间
		String startTime = format .format(calendar.getTime());
		int sumDevice = 0;
		for ( int i = 0; i <= selectTime * 12; i++ ){
			if(i == 0){
				//第一次查询的所有在线设备
				Query firstOnlineQuery = new Query();
				if(productId == null || productId.equals("")){
					firstOnlineQuery.addCriteria(Criteria.where("changeTime").lte(startTime).and("deviceStatus").is(1));
				}else{
					firstOnlineQuery.addCriteria(Criteria.where("changeTime").lte(startTime).and("productId").is(productId).and("deviceStatus").is(1));
				}
				List<DeviceRecord> deviceRecordOnlineList = mongoTemplate.find(firstOnlineQuery, DeviceRecord.class, "device_record");
				int countDeviceOnline = deviceRecordOnlineList.size();
				//第一次查询的所有离线设备
				Query firstOfflineQuery = new Query();
				if(productId == null || productId.equals("")){
					firstOfflineQuery.addCriteria(Criteria.where("changeTime").lte(startTime).and("deviceStatus").is(0));
				}else{
					firstOfflineQuery.addCriteria(Criteria.where("changeTime").lte(startTime).and("productId").is(productId).and("deviceStatus").is(0));
				}
				List<DeviceRecord> deviceRecordOfflineList = mongoTemplate.find(firstOfflineQuery, DeviceRecord.class, "device_record");
				int countOfflineDevice = deviceRecordOfflineList.size();
				//计算第一次查询时刻的在线设备数量
				sumDevice = countDeviceOnline - countOfflineDevice;
				String[] time = startTime.split(" ");
				String pointTime = time[1].substring(0, time[1].lastIndexOf(":"));
				hashMap.put(pointTime,sumDevice);
			}else{
				Date date = null;
				try {
					date = format.parse(startTime);
				} catch (Exception ex)
				{
					ex.printStackTrace();
				}
				//开始查询后的五分钟
				calendar.setTime(date);
				calendar.add(Calendar.MINUTE, 5);
				String nextTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(calendar.getTime());
				Query query =new Query();
				// 创建复合查询对象
				Criteria cri = new Criteria();
				// 设置条件
				if(productId == null || productId.equals("")){
					cri.and("deviceStatus").is(1);
				}else{
					cri.and("productId").is(productId);
					cri.and("deviceStatus").is(1);
				}
				// 时间段查询
				cri.and("changeTime").gte(startTime).lte((nextTime));
				query.addCriteria(cri);
				int countOnLineDevice =  mongoTemplate.find(query, DeviceRecord.class, "device_record").size();
				Query queryOffline =new Query();
				// 创建复合查询对象
				Criteria criOffline = new Criteria();
				// 设置条件
				if(productId == null || productId.equals("")){
					criOffline.and("deviceStatus").is(0);
				}else{
					criOffline.and("productId").is(productId);
					criOffline.and("deviceStatus").is(0);
				}

				// 时间段查询
				criOffline.and("changeTime").gte(startTime).lte((nextTime));
				queryOffline.addCriteria(criOffline);
				int countOfflineDevice =  mongoTemplate.find(queryOffline, DeviceRecord.class, "device_record").size();
				int onLineDevice = sumDevice + countOnLineDevice - countOfflineDevice;
				sumDevice = onLineDevice;
				startTime = nextTime;
				String[] time = startTime.split(" ");
				String pointTime = time[1].substring(0, time[1].lastIndexOf(":"));
				hashMap.put(pointTime,sumDevice);
			}
		}
		return hashMap;
	}
}
